package com.xbongbong.saas.enums.business;/*/*/

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 工单枚举类
 * @author 魏荣杰
 * @date 2019/3/12 15:47
 * @since v1.0
 * @version v1.0
 */
@Getter
public enum WorkOrderEnum {
    /**
     * 工单系统模板枚举值
     */
    SHEET_NO("serialNo", "enum.workOrder.attrName.sheet_no", "workOrderNo", FieldTypeEnum.SERIALNO.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    NAME("text_13", "enum.workOrder.attrName.name", "name", FieldTypeEnum.TEXT.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_CUSTOMER("text_1", "enum.workOrder.attrName.link_customer", "linkCustomer", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_CONTACT("text_2", "enum.workOrder.attrName.link_contact", "linkContact", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_OPPORTUNITY("text_3", "enum.workOrder.attrName.link_opportunity", "linkOpportunity", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_CONTRACT("text_4", "enum.workOrder.attrName.link_contract", "linkContract", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    CONTACT_INFORMATION("subForm_1", "enum.workOrder.attrName.contact_information", "contactInformation", FieldTypeEnum.CONTACT_NUMBER.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    SPARE_PARTS("array_1", "enum.workOrder.attrName.spare_parts", "products", FieldTypeEnum.SELECT_PRODUCT.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    SHIP_STATUS("text_70", "enum.workOrder.attrName.ship_status", "shipStatus", FieldTypeEnum.COMBO.getType(), 3, 1, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9925, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADDRESS("address_1", "enum.workOrder.attrName.address", "address", FieldTypeEnum.ADDRESS.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 1, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    SERVICE_PRICE("num_1", "enum.workOrder.attrName.service_price", "servicePrice", FieldTypeEnum.NUM.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    IMPLEMENTATION_TIME("date_1", "enum.workOrder.attrName.implementation_time", "implementationTime", FieldTypeEnum.DATETIME.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    MEMO("text_5", "enum.workOrder.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    EMERGENCY_LEVEL("text_6", "enum.workOrder.attrName.emergency_level", "emergencyLevel", FieldTypeEnum.COMBO.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_CUSTOMER_HIDE("text_7", "enum.workOrder.attrName.link_customer_hide", "linkCustomerHide", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    LINK_CONTACT_HIDE("text_8", "enum.workOrder.attrName.link_contact_hide", "linkContactHide", FieldTypeEnum.TEXT.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    LINK_OPPORTUNITY_HIDE("text_9", "enum.workOrder.attrName.link_opportunity_hide", "linkOpportunityHide", FieldTypeEnum.TEXT.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    LINK_CONTRACT_HIDE("text_10", "enum.workOrder.attrName.link_contract_hide", "linkContractHide", FieldTypeEnum.TEXT.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    MANAGER_ID("text_11", "enum.workOrder.attrName.manager_id", "managerId", FieldTypeEnum.USER.getType(), 3, 0, 1, ShowTypeEnum.EDIT.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),

    TIME_OUT("text_12", "enum.workOrder.attrName.time_out", "timeOut", FieldTypeEnum.WORK_ORDER_TIME_OUT.getType(), 3, 0, 1, ShowTypeEnum.LIST.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    STATUS("status", "enum.workOrder.attrName.status", "status", FieldTypeEnum.WORK_ORDER_STATUS.getType(), 3, 0, 1, ShowTypeEnum.LIST.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    STAGE("stage_id", "enum.workOrder.attrName.stage", "stage", FieldTypeEnum.WORK_ORDER_STAGE.getType(), 3, 0, 1, ShowTypeEnum.LIST.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    OWNER_ID("ownerId", "enum.workOrder.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9700, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    COUSERID("coUserId", "enum.workOrder.attrName.couserid", "coUserId", FieldTypeEnum.COUSERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9690, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    CREATOR_ID("creatorId", "enum.workOrder.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9710, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADD_TIME("addTime", "enum.workOrder.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9680, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME("updateTime", "enum.workOrder.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.LIST_DETAIL.getCode(), 9670, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    COMPLETE_TIME("endTime", "enum.workOrder.attrName.complete_time", "endTime", FieldTypeEnum.ENDTIME.getType(), 3, 0, 1, ShowTypeEnum.LIST_DETAIL.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    CLOSE_TIME("closeTime", "enum.workOrder.attrName.close_time", "closeTime", FieldTypeEnum.CLOSETIME.getType(), 3, 0, 1, ShowTypeEnum.LIST_DETAIL.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    RATE_STAR("num_2", "enum.workOrder.attrName.rate_star", "rateStar", FieldTypeEnum.STAR.getType(), 3, 0, 1, ShowTypeEnum.LIST_DETAIL.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ALL_OUTBOUND("allOutbound", "enum.workOrder.attrName.all_outbound", "allOutbound", FieldTypeEnum.WORK_ORDER_ALL_OUTBOUND.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9830, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    NODE_STATUS("num_3", "enum.workOrder.attrName.node_status", "num_3", FieldTypeEnum.WORK_ORDER_NODE_STATUS.getType(), 3, 0, 1, ShowTypeEnum.LIST.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    STAGE_NAME("text_14", "enum.workOrder.attrName.stage_name", "stageName", FieldTypeEnum.WORK_ORDER_STAGE_NAME.getType(), 1, 0, 1, ShowTypeEnum.LIST.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ;
    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see com.xbongbong.paas.enums.FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式详见
     *
     * @see com.xbongbong.saas.enums.ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    WorkOrderEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable, Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }
    /**
     * 对data内的字段拼上“data.”
     * @param cache
     * @return java.lang.String
     * @author zcp
     * @date 2019/3/1 18:12
     * @since v1.0
     */
    public static String getAttrConnectData(WorkOrderEnum cache) {
        return "data." + cache.getAttr();
    }
    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     * @author zcp
     * @date 2019/2/21 14:59
     * @since v1.0
     */
    public static String getEsAttr4Keyword(WorkOrderEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }
    /**
     * 拼接地址字段
     * 库内地址字段的格式为：data对象—address对象—{"province": "", "district":"", "city":"", "address": ""}
     * @param addressEnum
     * @param key 类型，此处传"province"、"district"、"city"
     * @return java.lang.String 处理后的es地址聚合字段
     * @author zcp
     * @date 2019/2/25 14:50
     * @since v1.0
     */
    public static String getAddress4Aggs(WorkOrderEnum addressEnum, String key) {
        String province = "province";
        String district = "district";
        String city = "city";
        String attr = "data." + addressEnum.getAttr() + ".";
        if (province.equals(key)) {
            return attr + province + ".keyword";
        } else if (district.equals(key)) {
            return attr + district + ".keyword";
        } else if (city.equals(key)) {
            return attr + city + ".keyword";
        }
        return attr + "keyword";
    }

    /**
     * 流程中获取隐藏字段
     * @return
     */
    public static List<WorkOrderEnum> getDisableField() {
        List<WorkOrderEnum> workOrderEnumList = new ArrayList<>();
        for (WorkOrderEnum workOrderEnum : values()) {
            if (Objects.equals(workOrderEnum.getShowType(), ShowTypeEnum.NONE.getCode()) || Objects.equals(workOrderEnum.getEditHide(), 1)
                    || Objects.equals(workOrderEnum.getFieldType(), FieldTypeEnum.CREATORID.getType()) || Objects.equals(workOrderEnum.getFieldType(), FieldTypeEnum.ADDTIME.getType())) {
                workOrderEnumList.add(workOrderEnum);
            }
        }
        return  workOrderEnumList;
    }

    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     * @param attr 字段名
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/4 15:31
     * @since v1.0
     * @version v1.0
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, LINK_CUSTOMER.attr)) {
            return LINK_CUSTOMER_HIDE.attr;
        } else if (Objects.equals(attr, LINK_CONTACT.attr)) {
            return LINK_CONTACT_HIDE.attr;
        } else if (Objects.equals(attr, LINK_OPPORTUNITY.attr)) {
            return LINK_OPPORTUNITY_HIDE.attr;
        } else if (Objects.equals(attr, LINK_CONTRACT.attr)) {
            return LINK_CONTRACT_HIDE.attr;
        }
        return attr;
    }

    /**
     * app端详情默认标题显示字段
     *
     * @author long.rao
     * @date 2019-07-12 01:29
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(SHEET_NO);
    }

    /**
     * 默认摘要
     *
     * @author long.rao
     * @date 2019-07-11 19:00
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(OWNER_ID, IMPLEMENTATION_TIME, ADDRESS);
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }
}
